#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gm.api import get_symbols

from web.api.base_api import BaseApi
from web.constants.python_api_result import PythonApiResult


class StockInfoApi(BaseApi):
    """
    stock_info表的API
    """

    def update_name_in_stock_info(self, date):
        """
        更新stock_info表的name字段
        """

        stock_info_list = self.stock_info_service.find_all()
        if stock_info_list is not None and len(stock_info_list) > 0:
            stock_info_name_str = str()
            for stock_info in stock_info_list:
                if stock_info.url_param[0:2].upper() == 'SH':
                    stock_info_name_str = stock_info_name_str + 'SHSE.' + stock_info.code_ + ","
                if stock_info.url_param[0:2].upper() == 'SZ':
                    stock_info_name_str = stock_info_name_str + 'SZSE.' + stock_info.code_ + ","
            stock_info_name_str = stock_info_name_str[:-1]

            result_list = get_symbols(sec_type1=1010, symbols=stock_info_name_str, trade_date=date, )
            if result_list is not None and len(result_list):
                for row in result_list:
                    code = row["symbol"].split(".")[1]
                    name = row["sec_name"]

                    self.stock_info_service.update_name_by_code(code, name)

        print(PythonApiResult.FINISH)